#!/bin/bash
## only support linux system, other system: https://www.terraform.io/cli/config/config-file#locations
info=`uname -s`
terraform_version="0.12.31"

if ! command -v unzip &> /dev/null
then 
  echo "Please install unzip package"
  exit 1
fi
      
case $info in
Linux)
    export platform="linux"
    ;;
Darwin)
    export platform="darwin"
    ;;
*)
    echo "The platform is not supported"
    exit 1
    ;;
esac

echo "Checking terraform..."
if ! command -v terraform &> /dev/null
then
    wget https://releases.hashicorp.com/terraform/${terraform_version}/terraform_${terraform_version}_${platform}_amd64.zip && \
    mkdir ~/terraform && \
    unzip -d ~/terraform -o terraform_${terraform_version}_${platform}_amd64.zip && \
    sudo install ~/terraform/terraform /usr/local/bin/terraform
    rm -r ~/terraform
fi

if [ ! -f ~/.terraformrc ]; then
cat > ~/.terraformrc << EOF
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
EOF
fi

if [ ! -d "$HOME/.terraform.d/plugin-cache" ];then
  mkdir -p $HOME/.terraform.d/plugin-cache
fi